package com.jiepos.mpos.biz.user.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jiepos.mpos.biz.code.service.NoticeInfService;
import com.jiepos.mpos.biz.user.service.LoginService;
import com.jiepos.mpos.common.constant.ExceptionConstant;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.core.controller.BaseController;
import com.jiepos.mpos.core.entity.MposResponse;
import com.jiepos.mpos.core.exception.MposException;
import com.jiepos.mpos.core.util.RandomValidateCode;

/**
 * @ClassName: LoginController
 * @Description: 用户登陆控制层
 * @author 孙永
 * @date 2017年9月26日 下午3:08:20
 */
@Controller
@RequestMapping("userlogin")
public class LoginController extends BaseController{
	
	@Autowired
	private LoginService loginService;
	@Autowired
	private NoticeInfService noticeInfService;
	
	/**
	 * 获取验证码
	 * @param response
	 * @param request
	 * @return
	 */
	@RequestMapping("/authCode.do")
	@ResponseBody
	public String getAuthCode(HttpServletResponse response, HttpServletRequest request){
		response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Set-Cookie", "name=value; HttpOnly");//设置HttpOnly属性,防止Xss攻击
		response.setDateHeader("Expire", 0);
		try{
			RandomValidateCode.getRandcode(request, response);// 输出图片方法
		}catch(Exception e){
			e.printStackTrace();
		}
		return "";
	}
	
	/**
	 * 用户登陆
	 * @param request
	 * @param loginName 登陆账号
	 * @param password 登陆密码
	 * @return
	 */
	@RequestMapping("/login.do")
	@ResponseBody
	public MposResponse login(HttpServletRequest request, String loginName, String password){
		MposResponse resp = new MposResponse();
		try{
			loginService.checkLogin(request, loginName, password);
		}catch(Exception e){
			resp = new MposResponse(ExceptionConstant.ERROR);
			if(e instanceof MposException){
				resp.setRespCode(((MposException) e).getCode());
				resp.setRespMsg(((MposException) e).getMessage());
			}
		}
		return resp;
	}
	
	/**
	 * 修改登陆密码页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/modifyLoginPwd.do")
	public ModelAndView modifyLoginPwd(HttpServletRequest request){
		ModelAndView view = new ModelAndView("/module/user/user/userinf_pwd");
		view.addObject("loginName", request.getSession().getAttribute(SystemConstant.SESSION_LOGIN_USER));
		return view;
	}
	
	/**
	 * 修改密码
	 * @param loginName 登陆账号
	 * @param oldPwd 原密码
	 * @param newPwd 新密码
	 * @return
	 */
	@RequestMapping("/modifyPwd.do")
	@ResponseBody
	public MposResponse modifyLoginPwd(HttpServletRequest request, String loginName, String oldPwd, String newPwd){
		MposResponse resp = new MposResponse();
		try{
			loginService.modifyLoginPwd(loginName, oldPwd, newPwd);
			request.getSession().invalidate();
		}catch(Exception e){
			resp = new MposResponse(ExceptionConstant.ERROR);
			if(e instanceof MposException){
				resp.setRespCode(((MposException) e).getCode());
				resp.setRespMsg(((MposException) e).getMessage());
			}
		}
		return resp;
	}
	
	/**
	 * 跳转主页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/main.do")
	public ModelAndView main(HttpServletRequest request, HttpServletResponse response){
		ModelAndView model = new ModelAndView("/common/main");
		model.addObject("notices", noticeInfService.findListForHomePage(request));
		model.addObject("notice", noticeInfService.findByUuidForHomePage(null));
		model.addAllObjects(loginService.getUserInf(request));
		return super.getDetail(request, response, model);
	}
	
	/**
	 * 退出登陆
	 * @param request
	 * @param response
	 */
	@RequestMapping("/loginOut.do")
	public void loginOut(HttpServletRequest request, HttpServletResponse response){
		try {
			request.getSession().invalidate();
			String contextPath = request.getContextPath();
			if(StringUtils.isBlank(request.getContextPath())){
				contextPath = "/";
			}
			response.sendRedirect(contextPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/checksession.do")
	@ResponseBody
	public boolean checksession(HttpServletRequest request, HttpServletResponse response){return true;}
	
}
